Airsim.Commands

Multicopter higher level commands for the Airsim simulator
from fastcore.utils import *

from UAV.airsim.client import AirSimClient
import UAV.airsim_python_client as airsim
import UAV.params as params
import time
from UAV.utils.sim_linux import RunSim, is_process_running, find_and_terminate_process
import logging
from UAV.airsim.commands import *
logging.basicConfig(format='%(asctime)-8s,%(msecs)-3d %(levelname)5s [name] [%(filename)10s:%(lineno)3d] %(message)s',
                    datefmt='%H:%M:%S',
                    level=params.LOGGING_LEVEL) 
logger = logging.getLogger(params.LOGGING_NAME)
from UAV.utils.display import *
from UAV.utils.sim_linux import *
from matplotlib import pyplot as plt

start_sim

 start_sim ()

Start the Airsim simuator if it is not already running


DroneCommands.arm

 DroneCommands.arm ()

Run the drone on a path in the Airsim simulator. Creates a client and connects to the simulator.


DroneCommands.takeoff

 DroneCommands.takeoff ()

Takeoff to the takeoff height


DroneCommands.do_NH_path

 DroneCommands.do_NH_path ()

Fly on a path in the Airsim simulator


DroneCommands.rth

 DroneCommands.rth ()

DroneCommands.land

 DroneCommands.land ()

DroneCommands.disarm

 DroneCommands.disarm ()

Disarm the drone and disconnect from the simulator


DroneCommands.do_tasklist

 DroneCommands.do_tasklist ()

Run a list of tasks in order


DroneCommands.stop

 DroneCommands.stop ()

stop the client by cancelling the last task and exiting the do_tasklist loop

An end to end example

The drone cammands are run in a separate process with the video loop running in the main process.

RECORD_VIDEO = False

import threading
from UAV.airsim.commands import DroneCommands
from UAV.airsim.client import  AirSimClient
import UAV.airsim_python_client as airsim
from UAV.utils.sim_linux import RunSim
from UAV.utils.display import puttext, VideoWriter, ScrollingLog, ScrollingLogHandler
from imutils import resize
import cv2
import logging
import time
# logging.basicConfig(format=
#                     '%(asctime)-8s,%(msecs)-3d %(levelname)5s [%(filename)10s:%(lineno)3d] %(message)s',
#                     datefmt='%H:%M:%S',
#                     level=logging.INFO)  
import UAV.params as params
logger = logging.getLogger(params.LOGGING_NAME) # Todo add this to params
logger.setLevel(params.LOGGING_LEVEL)
# log = ScrollingLog(bg_color=(0,0,0))
log = ScrollingLog(position=(20,80), font_scale=1.5 , color=(0,0,255), thickness=1)
handler_log = ScrollingLogHandler(log, logger)
logger.info(f"Hello World...")

rs = RunSim("AirSimNH", settings="config/settings_high_res.json")

asc = AirSimClient()
cmd = DroneCommands()
t = threading.Thread(target=cmd.do_tasklist, daemon=True)
t.start()

framecounter = 1
cam_num = 0
cams = ["high_res", "front_center", "front_right", "front_left", "bottom_center", "back_center"]
# with VideoWriter("images/airsim_test.mp4", 5.0) as video:
if RECORD_VIDEO:
    video = VideoWriter("images/airsim_nav_test.mp4", 5.0)
else:
    video = None
# with VideoWriter("images/airsim_nav_test.mp4", 25.0) as video:
if True:
    while(True):
        framecounter += 1
        state = asc.getMultirotorState()
        pos = state.kinematics_estimated.position
        img = asc.get_image(cams[cam_num], rgb2bgr=False)
        puttext(img, f"Frame: {framecounter} Pos: {pos.x_val:.2f}, {pos.y_val:.2f}, {pos.z_val:.2f}")
    
        img = resize(img, width=800)    
        # log.update(f"Frame: {framecounter} Pos: {pos.x_val:.2f}, {pos.y_val:.2f}, {pos.z_val:.2f}")
        if framecounter % 100 == 0:
            print(f"Frame: {framecounter} Pos: {pos.x_val:.2f}, {pos.y_val:.2f}, {pos.z_val:.2f}")
        log.draw(img)
        cv2.imshow("Camera", img)
        if video is not None: video.add(img)
        
        # video.add(img_bgr)
        k = cv2.waitKey(10)
        if k == ord('q') or k == ord('Q'):
            # logger.info("......cancelLastTask")
            asc.cancelLastTask()
            # print(f"Landed state:  {state.landed_state}")
            if state.landed_state == 0:
                logger.info("Landed state = 0,  so quiting")
                break  
        
        if k == ord('c') or k == ord('C'):
            cam_num += 1
            if cam_num >= len(cams):
                cam_num = 0
            # log.update(f"Camera: {cams[cam_num]}")
            logger.info(f"Camera: {cams[cam_num]}")
            
        if k == 27:
            cmd.stop()
            time.sleep(1)
            break
            
        # if framecounter > 50:
        #     break

cmd.disarm()
t.join(timeout=5)
cv2.destroyAllWindows()
rs.exit()

if video is not None: 
    video.close()
    video.show(width=500)
INFO   | uav_log         | 13.731 | 2696093584.py: 23 | MainThread         | Hello World...
INFO   | uav_log         | 16.754 | commands.py: 46 | Thread-6 (do_taskl | Arming the drone...
INFO   | uav_log         | 16.755 | commands.py: 61 | Thread-6 (do_taskl | taking off...
INFO   | uav_log         | 22.300 | commands.py: 82 | Thread-6 (do_taskl | flying on path...
INFO   | uav_log         | 36.535 | commands.py: 93 | Thread-6 (do_taskl | returning home...
INFO   | uav_log         | 39.553 | commands.py: 99 | Thread-6 (do_taskl | landing...
INFO   | uav_log         | 41.276 | commands.py: 52 | Thread-6 (do_taskl | disarming...
INFO   | uav_log         | 45.054 | 2696093584.py: 64 | MainThread         | Landed state = 0,  so quiting
INFO   | uav_log         | 45.055 | commands.py: 52 | MainThread         | disarming...
Settings file config/settings_high_res.json not found.
Starting Airsim  ['/home/jn/Airsim/AirSimNH/LinuxNoEditor/AirSimNH/Binaries/Linux/AirSimNH', '-ResX=800', '-ResY=600', '-windowed']
Started Airsim AirSimNH
Connected!
Client Ver:1 (Min Req: 1), Server Ver:1 (Min Req: 1)

Writing video to /home/jn/PycharmProjects/UAV/nbs/api/images/airsim_nav_test.mp4 at 5.0 fps.
Connected!
Client Ver:1 (Min Req: 1), Server Ver:1 (Min Req: 1)
make sure we are hovering at 5 meters...
This script is designed to fly on the streets of the Neighborhood environment
            and assumes the unreal position of the drone is [160, -1500, 120].
Frame: 100 Pos: 41.01, -0.00, -6.36
Frame: 200 Pos: 126.92, -33.09, -10.31
Airsim exited with rc = 143
Video: /home/jn/PycharmProjects/UAV/nbs/api/images/airsim_nav_test.mp4
# rs.exit()